package register;

import com.common.RegisterTool;
import com.opensymphony.xwork2.ActionSupport;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import java.io.IOException;
import java.io.PrintWriter;
//import com.sun.rowset.CachedRowSetImpl;

public class CheckLogin extends ActionSupport{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String uname;
	private String email;
	private String phone;
	private String attr;
	
	public String getAttr() {
		return attr;
	}

	public void setAttr(String attr) {
		this.attr = attr;
	}

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	public String getUname() {
		return uname;
	}

	public void setUname(String uname) {
		this.uname = uname;
	}
	
	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public String execute() throws IOException{
		ServletActionContext.getResponse().setContentType("text/html;charset=utf-8");
		PrintWriter writer = ServletActionContext.getResponse().getWriter();
		ApplicationContext context = new ClassPathXmlApplicationContext("Spring-Module.xml");
		RegisterTool tool = (RegisterTool) context.getBean("registerTool");
        boolean flag = false;
        System.out.println("attr");
        //检查是否有重复的用户名、手机号或邮件
        if(attr.equals("userName")){
            System.out.println(uname);
            flag = tool.checkLogin("select * from RegisterAccount where username = ?", uname) ||
                    tool.checkLogin("select * from Account where username = ?", uname);
		}else if(attr.equals("Email")){
            System.out.println(email);
            flag = tool.checkLogin("select * from RegisterAccount where Email = ?", email) ||
                    tool.checkLogin("select * from Account where Email = ?", email);
		}else if(attr.equals("Telephone")){
            System.out.println(phone);
            flag = tool.checkLogin("select * from RegisterAccount where Telephone = ?", phone) ||
                    tool.checkLogin("select * from Account where Telephone = ?", phone);
		}

		if (flag) writer.write("true");
		else writer.write("false");

		return null;
	}
	
}
